# -*- coding:utf-8 -*-

from bs4 import BeautifulSoup
import csv
import requests
import sys

reload(sys)
sys.setdefaultencoding('utf-8')
url = 'http://cq.lianjia.com/zufang/shapingba/pg{page}/'

page = 0

csv_file = open('fang.csv', 'wb')
csv_write = csv.writer(csv_file, delimiter=',')

while True:
    page += 1
    print '正在爬取网页:', url.format(page=page)
    response = requests.get(url.format(page=page))
    html = BeautifulSoup(response.content, 'html.parser')
    house_list = html.find('div', {'class', 'list-wrap'}).find_all('div', {'class', 'info-panel'})
    if not house_list:
        print '页面找不到'
        break
    for house in house_list:
        house_title = house.find('div', {'class', 'where'}).get_text()
        house_url = house.find('div',{'class','where'}).a['href']
        house_location = house.find('div',{'class','con'}).a.text
        house_money = house.find('div',{'class','price'}).span.text
        csv_write.writerow([house_title,house_location,house_money,house_url])

csv_file.close()